class Solution {
    public int reversePairs(int[] nums) {

        return mer(nums,0,nums.length-1);

    }

    public int mer(int[] nums,int l,int r){
        if(l>=r) return 0;
        int ret=0, mid=(r+l)/2;

        ret+=mer(nums,l,mid);
        ret+=mer(nums,mid+1,r);
        int left=l,right=mid+1,i=0;
        int[] tmp=new int[r-l+1];
        while(left<=mid&&right<=r){
            if(nums[left]/2.0<=nums[right]) left++;
            else{
                ret+=mid-left+1;
                right++;
            }
        }


        left=l;right=mid+1;
        while(left<=mid&&right<=r){
            if(nums[left]<=nums[right]) tmp[i++]=nums[left++];
            else{
                tmp[i++]=nums[right++];
            }
        }
        while(left<=mid) tmp[i++]=nums[left++];
        while(right<=r) tmp[i++]=nums[right++];

        for(i=0;i<(r-l+1);i++) nums[l+i]=tmp[i];

        return ret;


    }




}